package com.cmii.sjw.department.control.jsc.controller;

import com.cmii.sjw.department.control.jsc.model.ActivityCode;
import com.cmii.sjw.department.control.jsc.model.dto.TrainingParticipantsDTO;
import com.cmii.sjw.department.control.jsc.model.entity.TrainingUniversities;
import com.cmii.sjw.department.control.jsc.model.params.TrainingParams;
import com.cmii.sjw.department.control.jsc.service.TrainingService;
import com.cmii.sjw.department.control.jsc.service.impl.LoginService;
import com.cmii.sjw.department.control.jsc.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数字重庆建设能力提升培训参会回执
 * @Author: jfh
 * @Date: 2022/03/07 15:07
 * @Description: 数字重庆建设能力提升培训参会回执
 * @Version: 1.0
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/training")
public class TrainingController {

    @Autowired
    private TrainingService trainingService;

    @Autowired
    private LoginService loginService;

    /**
     * 获取高校列表
     * @return 高校列表
     */
    @PostMapping("/universitiesList")
    public Result<List<TrainingUniversities>> universitiesList() {
        return trainingService.universitiesList();
    }

    /**
     * 参训人员信息保存
     * @param params 参训人员信息
     * @return 保存结果
     */
    @PostMapping("/saveTrainingParticipants")
    public Result<String> saveTrainingParticipants(@RequestBody TrainingParams params) {

//        String captcha = loginService.validateCaptcha(params.getCode(), params.getUuid());
//        // 验证码校验
//        if ("expire".equals(captcha)){
//            return Result.error("验证码到期");
//        }else if ("error".equals(captcha)){
//            return Result.error("验证码错误");
//        }

        List<TrainingParticipantsDTO> tpds = params.getTpds();
        log.info("参训人员信息保存: {}", tpds);
        if (tpds.isEmpty()) {
            return Result.error("参训人员信息不能为空");
        }
        if  (tpds.size() > 2) {
            return Result.error("参训人员信息单次不能超过2条");
        }
        return trainingService.saveTrainingParticipants(tpds);
    }

    /**
     * 获取高校授权码
     * @param universitiesName 高校名称
     * @return 高校授权信息
     */
    @PostMapping("/getUniversitiesAuthCode")
    public Result<List<ActivityCode>> getUniversitiesAuthCode(String universitiesName) {

        log.info("高校名称: {}", universitiesName);

        if (StringUtils.isBlank(universitiesName)) {
            return Result.error("高校名称不能为空");
        }

        return trainingService.getUniversitiesAuthCode(universitiesName);
    }

    /**
     * 创建高校授权码
     * @param params 高校授权信息
     * @return 创建结果
     */
    @PostMapping("/createUniversitiesAuthCode")
    public Result<String> createUniversitiesAuthCode(@RequestBody ActivityCode params) {

        log.info("高校授权信息: {}", params);

        if (StringUtils.isBlank(params.getSchoolName())) {
            return Result.error("高校名称不能为空");
        }
        /*if (StringUtils.isBlank(params.getApplicationSystemName())) {
            return Result.error("应用系统不能为空");
        }*/

        return trainingService.createUniversitiesAuthCode(params);
    }

}
